package com.oscarmovie.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;

public class Person {
	private int id;
	private String name;
	private String nameCn;
	private Date startDate;
	private Date endDate;
	private String biography;
	private int doubanId;
	private int mtimeId;
	private String alias;
	
	private Set<Mpr> mprs;
	//one-to-many
	private Set<Image> images;
	private Image image;
	private String displayName;
	private Set<Mpac> mpacs;
	
	private Statistics statistics;
	private Set<CommentPerson> comments;
	
	public String getDisplayName() {
		if (displayName == null) {
			displayName = name;
			if (nameCn!=null && !"".equals(nameCn)) {
				displayName = nameCn;
			}
		}
		return displayName;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNameCn() {
		return nameCn;
	}
	public void setNameCn(String nameCn) {
		this.nameCn = nameCn;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	public void setMprs(Set<Mpr> mprs) {
		this.mprs = mprs;
	}
	public Set<Mpr> getMprs() {
		return mprs;
	}
	public void setBiography(String biography) {
		this.biography = biography;
	}
	public String getBiography() {
		return biography;
	}
	public void setImages(Set<Image> images) {
		this.images = images;
	}
	public Set<Image> getImages() {
		return images;
	}
	public void setImage(Image image) {		
		this.image = image;
	}
	public Image getImage() {
		
		return image;
	}
	public void setMpacs(Set<Mpac> mpacs) {
		this.mpacs = mpacs;
	}
	public Set<Mpac> getMpacs() {
		return mpacs;
	}
	
	public List<Mpac> getSortedMpacs() {
		List<Mpac> sortedMpacs = new ArrayList();
		for (Mpac mpac : getMpacs()) {
			sortedMpacs.add(mpac);
		}
		
		Collections.sort(sortedMpacs, new Comparator<Mpac>(){

			
			public int compare(Mpac o1, Mpac o2) {
				if (o1.getCalendar().getDatetime().before(o2.getCalendar().getDatetime())) {
					return 1;					
				} else if (o1.getCalendar().getDatetime().after(o2.getCalendar().getDatetime())) {
					return -1;
				}	
								
				return 0;
			}
		});
		return sortedMpacs;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!getClass().isAssignableFrom( obj.getClass()))
			return false;
		Person other = (Person) obj;
		if (getId() != other.getId())
			return false;
		return true;
	}
	public void setMtimeId(int mtimeId) {
		this.mtimeId = mtimeId;
	}
	public int getMtimeId() {
		return mtimeId;
	}
	public void setDoubanId(int doubanId) {
		this.doubanId = doubanId;
	}
	public int getDoubanId() {
		return doubanId;
	}
	public void setAlias(String alias) {
		this.alias = alias;
	}
	public String getAlias() {
		return alias;
	}
	public void setStatistics(Statistics statistics) {
		this.statistics = statistics;
	}
	public Statistics getStatistics() {
		return statistics;
	}
	public void setComments(Set<CommentPerson> comments) {
		this.comments = comments;
	}
	public Set<CommentPerson> getComments() {
		return comments;
	}
}
